﻿/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//Copyright (c) 2009 foldier inc.
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
//associated documentation files (the "Software"), to deal in the Software without restriction, including 
//without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
//copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
//following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions
//of the Software.
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
//IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Configuration;
using System.Collections.Generic;
using System.Text.RegularExpressions;

#if SILVERLIGHT
using System.Windows.Browser;
#else
using System.Web;
using System.Xml.XPath;
#endif

namespace foldier.API
{
	/// <summary>
    /// BaseApi derived class.
    /// Implements sPression items API calls
	/// </summary>
    public class sPress : BaseApi
	{
		
		#region Protected local members

		protected long m_spressID = -1;

		#endregion

		#region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connector">FoldierOAuthConnector object for authentication</param>
		public sPress(FoldierOAuthConnector connector) : base(connector)
		{
		}
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connector">FoldierOAuthConnector object for authentication</param>
        /// <param name="spressID">sPress ID</param>
		public sPress(FoldierOAuthConnector connector, long spressID ) : base(connector)
		{
			m_spressID = spressID;
		}

		#endregion

		#region API CALLS

		/// <summary>
        /// Returns the single ItemSPression object.
		/// </summary>
        /// <returns>ItemSPression object</returns>
		public ItemSPression get() { return get(m_spressID); }
		
        /// <summary>
        /// Returns the single ItemSPression object given the sPression ID
		/// </summary>
		/// <param name="sPressID">sPression ID</param>
        /// <returns>ItemSPression object</returns>
        public ItemSPression get(long sPressID)
		{
			string result = string.Empty;
			if (sPressID <= 0)
				return null;

			result = m_connection.genericApiCall(string.Format("{0:x}/spress/{1:x}?out=full&format=xml", m_connection.UserID, sPressID ), string.Empty);

			return m_connection.resultItemSPression(result);

		}

		/// <summary>
        /// Retrieve the ItemLink array of all elements within a specified sPression.
        /// Data returned from most recent.
		/// </summary>
		/// <returns>ItemLink array</returns>
		public ItemLink[] getLinks() { return getLinks(m_spressID, 0, 20); }
		
        /// <summary>
        /// Retrieve the ItemLink array of all elements within a specified sPression.
        /// Data returned from most recent.
		/// </summary>
		/// <param name="sPressID">sPression ID</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks( long sPressID ) { return getLinks(sPressID, 0, 20); }
		
        /// <summary>
        /// Retrieve the ItemLink array of all elements within a specified sPression.
        /// Data returned from most recent.
        /// </summary>
        /// <param name="start">Index of first ItemLink to return</param>
        /// <param name="count">Number of ItemLink to return</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks( int start, int count ) { return getLinks(m_spressID, start, count); }
		
        /// <summary>
        /// Retrieve the ItemLink array of all elements within a specified sPression
        /// Data returned from most recent.
		/// </summary>
		/// <param name="sPressID">sPression ID</param>
        /// <param name="start">Index of first ItemLink to return</param>
        /// <param name="count">Number of ItemLink to return</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks( long sPressID, int start, int count )
		{
			string result = string.Empty;
			if ( sPressID > 0 )
				result = m_connection.genericApiCall( string.Format("{0:x}/spress/{1:x}?start={2}&count={3}&format=xml&sort=ModifiedNewToOld&out=fullcontent"
													, m_connection.UserID, sPressID, start, count), string.Empty);
			else
				result = m_connection.genericApiCall( string.Format("{0:x}/spress?start={2}&count={3}&format=xml&sort=ModifiedNewToOld&out=fullcontent"
													, m_connection.UserID, start, count), string.Empty);

			return m_connection.resultItemLinks(result); 
		}

		/// <summary>
        ///		Retrieves the ItemSPression array of all the sPression within the connected account.
        ///		Data returned in Alphabetical order.
		/// </summary>
        /// <returns>ItemSPression array</returns>		
		public ItemSPression [] getAll() { return getAll( 0, 20 ); }
		
        /// <summary>
        /// Retrieves the ItemSPression array of all the sPression within the connected account.
        ///		Data returned from most recent in Alphabetical order.
		/// </summary>
		/// <param name="start">Index of first ItemSPression to return</param>
		/// <param name="count">Number of ItemSPression to return</param>
        /// <returns>ItemSPression array</returns>
        public ItemSPression [] getAll(int start, int count )
		{
            string result = m_connection.genericApiCall(string.Format("{0:x}/spress?start={1}&count={2}&format=xml&sort=AlphabeticalAToZ&out=full", m_connection.UserID, start, count)
													   , string.Empty );

			return m_connection.resultItemSPressions( result );

		}

        /// <summary>
        /// Search for ItemSPressions matching query.
        /// Data returned from most recent.
        /// </summary>
        /// <param name="query">Search query</param>
        /// <returns>ItemSPression array</returns>
		public ItemSPression[] search( string query ) { return search(query, 0, 20); }
		
        /// <summary>
        /// Search for ItemSPressions matching query.
        /// Data returned from most recent, output format is fullcontent XML
		/// </summary>
        /// <param name="query">Search query</param>
		/// <param name="start">Index of first ItemSPression to return</param>
		/// <param name="count">Number of ItemSPression to return</param>
        /// <returns>ItemSPression array</returns>
        public ItemSPression[] search( string query, int start, int count )
		{
            string result = m_connection.genericApiCall(string.Format("{0:x}/spress?q={1}&start={2}&count={3}&format=xml&sort=ModifiedNewToOld&out=full", m_connection.UserID, query, start, count)
													   , string.Empty );

			return m_connection.resultItemSPressions(result); 

		}
		
		/// <summary>
        /// Search for ItemLinks matching query.
        /// Data returned from most recent, output format is fullcontent XML
		/// </summary>
        /// <param name="query">Search query</param>
        /// <returns>ItemLink array</returns>
		public ItemLink[] searchLinks( string query ) { return searchLinks(m_spressID, query, 0, 20); }
		
        /// <summary>
        /// Search for ItemLinks matching query within a specific sPression ID.
        /// Data returned from most recent, output format is fullcontent XML
		/// </summary>
		/// <param name="sPressID">sPression ID</param>
		/// <param name="query">Search query</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] searchLinks( long sPressID, string query) { return searchLinks(m_spressID, query, 0, 20); }
		
        /// <summary>
        /// Search for ItemLinks matching query.
        /// Data returned from most recent, output format is fullcontent XML
		/// </summary>
        /// <param name="query">Search query</param>
		/// <param name="start">Index of first ItemLink to return</param>
		/// <param name="count">Number of ItemLink to return</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] searchLinks(string query, int start, int count) { return searchLinks(m_spressID, query, start, count); }
		
        /// <summary>
        /// Search for ItemLinks matching query within a specific sPression ID.
        /// Data returned from most recent, output format is fullcontent XML
		/// </summary>
        /// <param name="sPressID">sPression ID</param>
        /// <param name="query">Search query</param>
        /// <param name="start">Index of first ItemLink to return</param>
        /// <param name="count">Number of ItemLink to return</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] searchLinks( long sPressID, string query, int start, int count)
		{
			string result = string.Empty;
			if ( sPressID > 0 )
				result = m_connection.genericApiCall(string.Format("{0:x}/spress/{1:x}?q={2}&start={3}&count={4}&format=xml&out=fullcontent&sort=ModifiedNewToOld"
													, m_connection.UserID, sPressID, FoldierOauth.UriEncode( query ), start, count ), string.Empty );
			else
				result = m_connection.genericApiCall(string.Format("{0:x}/spress?q={1}&start={2}&count={3}&format=xml&out=fullcontent&sort=ModifiedNewToOld"
													, m_connection.UserID, FoldierOauth.UriEncode( query ), start, count ), string.Empty );

			return m_connection.resultItemLinks(result);
		}
        
		#endregion

    }
}
